package org.openkanban.client.ui.footer;

import org.openkanban.client.datasource.BoardHistoryDS;
import org.openkanban.client.ui.board.BoardComposite;
import org.openkanban.client.util.RootPanel;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

/**
 * GUI Element of the HistoryListGrid. Part of the BoardComposite. Must react of
 * a board switch.
 */
public class HistoryListGrid extends ListGrid implements BoardComposite {
    public HistoryListGrid() {
	ListGridField historyDateTime = new ListGridField("historyDateTime", "Date - Time", 100);
	ListGridField namensKuerzelHistory = new ListGridField("namensKuerzel", " ", 50);
	ListGridField historyActionValue = new ListGridField("historyActionValue", " ", 25);
	ListGridField text = new ListGridField("text", "Content");
	historyDateTime.setAlign(Alignment.CENTER);
	namensKuerzelHistory.setAlign(Alignment.CENTER);
	historyActionValue.setAlign(Alignment.CENTER);

	setWidth100();
	setHeight(146);
	setAutoFetchData(false);
	setCanDragRecordsOut(false);
	setCanAcceptDroppedRecords(false);
	setCanReorderRecords(false);
	setPreventDuplicates(true);
	setSortField("historyDateTime");
	setSortDirection(SortDirection.DESCENDING);

	setFields(historyDateTime, namensKuerzelHistory, historyActionValue, text);

	RootPanel.boardComponent.addComposite(this);
    }

    @Override
    public void switchBoard(Long boardId) {
	if (getDataSource() != null) {
	    getDataSource().destroy();
	    BoardHistoryDS.setInstanceNull();
	}
	setDataSource(BoardHistoryDS.getInstance(boardId.toString()));
	fetchData();
	filterData();
	redraw();
    }
}
